package com.bzm.controller.plugins;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bzm.common.entity.HttpCode;
import com.bzm.common.entity.JsonResult;
import com.bzm.common.enums.plugins.DataSourceEnum;
import com.bzm.common.utils.IpParser;
import com.bzm.common.utils.NetworkUtil;
import com.bzm.controller.base.BaseController;
import com.bzm.pojo.bzm.BzmActivity;
import com.bzm.pojo.bzm.BzmImUser;
import com.bzm.pojo.bzm.vo.BzmImChatMsgLogsVo;
import com.bzm.pojo.file.SysFileInfo;
import com.bzm.pojo.plugins.echarts.EchartsOption;
import com.bzm.pojo.plugins.im.ImRegisterBeat;
import com.bzm.service.bzm.BzmActivityService;
import com.bzm.service.bzm.BzmImUserService;
import com.bzm.service.plugins.BzmImCommonService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping(BaseController.BZM_PLUGIN)
public class BzmPluginIndexController {
	
	@Autowired
	private BzmImUserService bzmImUserService;
	@Autowired
	private BzmImCommonService bzmImCommonService;
	@Autowired
	private BzmActivityService bzmActivityService;	
	
	// index
	@RequestMapping(value="/index.html")
    public ModelAndView index(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();	
    	mv.setViewName("/plugin/bzm_plugin_index");
    	return mv;
    }
	
	
	// redis
	@RequestMapping(value="/redis.html")
    public ModelAndView redis(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();	
    	mv.setViewName("/plugin/bzm_plugin_redis");
    	return mv;
    }
	
	// im single
	@RequestMapping(value="/im/single.html")
    public ModelAndView imSingle(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();	
    	mv.setViewName("/plugin/bzm_plugin_im_single");
    	return mv;
    }
	
	// RabbitMQ
	@RequestMapping(value="/im/rabbitmq.html")
    public ModelAndView rabbitmq(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();	
    	mv.setViewName("/plugin/bzm_plugin_rabbitmq");
    	return mv;
    }
	
	// zookeeper
	
	// to url
	@GetMapping(value = "/to/url")
    public ModelAndView toUrl(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();	
		String toUrl = "/plugin/bzm_plugin_index";
		String pluginId = request.getParameter("pluginId");
		if(StringUtils.isNotBlank(pluginId)) {
			/*
			 * QueryWrapper<PluginInfo> queryWrapper = new QueryWrapper<PluginInfo>();
			 * queryWrapper.eq("plugin_id", pluginId); PluginInfo pluginInfo =
			 * pluginInfoService.getOne(queryWrapper); if(pluginInfo!=null) { toUrl =
			 * pluginInfo.getToUrl(); mv.addObject("pluginInfo", pluginInfo); }
			 */
		}
		mv.setViewName(toUrl);
    	return mv;
    }
	
	// login
	@PostMapping(value = "/login")
	@ResponseBody
	public JsonResult<BzmImUser> login(HttpServletRequest request){
		String phone = request.getParameter("phone");
		String nickname = request.getParameter("nickname");
		String avatar = request.getParameter("avatar");
		String reqIp = NetworkUtil.getIpAddress(request);
        IpParser.IpInfo info = IpParser.parseIp(reqIp);
		return bzmImUserService.login(phone,nickname,info,avatar);
	}
	
	// user list
	@GetMapping(value = "/user/list")
	@ResponseBody
	public JsonResult<List<BzmImUser>> userList(HttpServletRequest request){
		QueryWrapper<BzmImUser> queryWrapper = new QueryWrapper<BzmImUser>();
		List<BzmImUser> pluginList = bzmImUserService.list(queryWrapper);
		for (BzmImUser user : pluginList) {
			String username = user.getUsername();
			String newUsername = username.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
			user.setUsername(newUsername);
			String phone = user.getPhone();
			String newPhone = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
			user.setPhone(newPhone);
			
		}
		return new JsonResult<List<BzmImUser>>(pluginList, HttpCode.SUCCESS);
	}
	
	// login/out
	@PostMapping(value = "/login/out")
	@ResponseBody
	public JsonResult<BzmImUser> loginOut(HttpServletRequest request){
		String userId = request.getParameter("userId");
		return bzmImUserService.loginOut(userId);
	}
	
	// im 注册服务
	@GetMapping(value = "/im/register/service")
	@ResponseBody
	public JsonResult<ImRegisterBeat> imRegisterService(HttpServletRequest request){
		String code = request.getParameter("code");
		return bzmImCommonService.selectImRegisterService(code);
	}
	
	// im 注册列表
	@GetMapping(value = "/im/register/list")
	@ResponseBody
	public JsonResult<List<ImRegisterBeat>> imRegisterList(HttpServletRequest request){
		String code = request.getParameter("code");
		return bzmImCommonService.selectImRegisterList(code);
	}
	
	// im 用户头像
	@GetMapping(value = "/im/user/avatar")
	@ResponseBody
	public JsonResult<List<SysFileInfo>> imUserAvatarList(HttpServletRequest request){
		String code = DataSourceEnum.IM_USER_AVATAR.getCode();
		return bzmImCommonService.selectImUserAvatarList(code);
	}
	
	// 用户新增趋势 /count/user/add/
	@GetMapping(value = "/count/user/add")
	@ResponseBody
	public JsonResult<EchartsOption> countUserAdd(HttpServletRequest request){
		return bzmImCommonService.selectCountUserAdd(request);
	}
	
	// 消息新增趋势 /count/msg/add/
	@GetMapping(value = "/msg/msg/add")
	@ResponseBody
	public JsonResult<EchartsOption> countMsgAdd(HttpServletRequest request){
		return bzmImCommonService.selectCountMsgAdd(request);
	}
	
	// 查询群消息
	@GetMapping(value = "/group/msg/list")
	@ResponseBody
	public JsonResult<List<BzmImChatMsgLogsVo>> groupMsgList(HttpServletRequest request){
		String groupId = request.getParameter("groupId");
		return bzmImCommonService.selectGroupMsgList(groupId);
	}
	
	
	// 查询好友消息
	@GetMapping(value = "/friend/msg/list")
	@ResponseBody
	public JsonResult<List<BzmImChatMsgLogsVo>> friendMsgList(HttpServletRequest request){
		String sendId = request.getParameter("sendId");	
		String receiveId = request.getParameter("receiveId");	
		return bzmImCommonService.selectFriendMsgList(sendId,receiveId);
	}
	
	// 活动列表  bzmActivityService
	@GetMapping(value = "/activity/list")	
	@ResponseBody
	public JsonResult<List<BzmActivity>> activityList(HttpServletRequest request){
		return bzmActivityService.selectActivityList();
	}
	
}
